package com.js.nickcar.dialog;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.js.nickcar.R;
import com.js.nickcar.api.OptionConfig;
import com.js.nickcar.base.BaseHolder;
import com.js.nickcar.entity.remote.OptionBean;

import java.util.ArrayList;

/**
 * Created by LZY on 2017/9/29.
 */

public class OptionAdapter extends RecyclerView.Adapter<OptionAdapter.OptionHolder> implements OptionConfig {

    public final Context mContext;
    public final LayoutInflater mInflater;

    private ArrayList<OptionBean> mOptions = new ArrayList<>();
    private OptionBean mCurOption;

    public OptionAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(mContext.getApplicationContext());
    }

    @Override
    public OptionHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new OptionHolder(mInflater.inflate(R.layout.item_dialog_option, parent, false));
    }

    @Override
    public void onBindViewHolder(OptionHolder holder, int position) {
        final OptionBean item = getItem(position);
        holder.itemView.setSelected(item.equals(mCurOption));

        switch (item.getType()) {
            case CAR_CHECK_TECH_STATUS:
            case CAR_CHECK_FIX_STATUS:
            case CAR_CHECK_USAGE:
                holder.tvContent.setText(item.getDescription() + "(" + item.getValue() + ")");
                break;
            case CAR_CHECK_STATUS:
                holder.tvContent.setText(item.getDescription());
                break;
            default:
                holder.tvContent.setText(item.getValue());
                break;

        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurOption = item;
                notifyDataSetChanged();
            }
        });

    }

    public OptionBean getItem(int position) {
        return mOptions.get(position);
    }

    public OptionBean getCurOption() {
        return mCurOption;
    }

    @Override
    public int getItemCount() {
        return mOptions.size();
    }

    public void setCurOption(OptionBean curOptionBean) {
        mCurOption = curOptionBean;
    }

    public static class OptionHolder extends BaseHolder {

        public final TextView tvContent;

        public OptionHolder(View itemView) {
            super(itemView);
            tvContent = findViewById(R.id.tv_content);
        }
    }

    public void addAll(ArrayList<OptionBean> options) {
        if (options != null) {
            mOptions.clear();
            mOptions.addAll(options);
            notifyDataSetChanged();
        }
    }

}
